Ext.define('Zm.Common', {
    singleton: true,
    requires:['Ext.window.MessageBox','Zm.i18n.Lang','Zm.model.IdText','Zm.store.EnumStore','Zm.store.DataCol'],
    errMsg: function(stitle,smsg){
        Ext.Msg.show({
           title:stitle,
           msg: smsg,
           buttons: Ext.Msg.OK,
           icon: Ext.Msg.ERROR
        });
    },
    infoMsg:function(stitle,smsg){
        Ext.Msg.show({
           title:stitle,
           msg: smsg,
           buttons: Ext.Msg.OK,
           icon: Ext.Msg.INFO
        });
    },
    promptMsg:function(cfg, msg, fn, scope, multiline, value, opt){
        if (Ext.isString(cfg)) {
            cfg = {
                prompt: true,
                title: cfg,
                minWidth: Ext.Msg.minPromptWidth,
                msg: msg,
                buttons: Ext.Msg.OKCANCEL,
                callback: fn,
                scope: scope,
                multiline: multiline,
                value: value,
                opt:opt
            };
        }
        Ext.Msg.show(cfg);
    },

    onFail:function(form, action){
        var cm = Zm.Common;
        if(action.failureType == 'server'){ 
            obj = Ext.decode(action.response.responseText,true);
            if(obj!=null) cm.errMsg('失败', obj.msg); 
        }else{ 
            cm.errMsg('警告', '表单校验错误，请检查表单项！'); 
        }
    },

    onAFail:function(response,options){
        Zm.Common.errMsg('警告', '服务器不可到达，请检查网络是否畅通'); 
    },
    
    rendBool: function(val){
        if(val==1){
            return '是';
        }else{
            return '否';
        }
    },
    rendMoney:function(val){
        if(val === undefined) return '';
        var v = parseFloat(val);
        if(v==0 || v == Number.NaN) return '';
        return '￥ '+v.toFixed(2);
    },
    rendFloat:function(val){
        if(val === undefined) return '';
        var v = parseFloat(val);
        if(v==0 || v == Number.NaN) return '';
        return ''+v.toFixed(2);
    },
    rendPer:function(val){
        if(val === undefined) return '';
        var v = parseFloat(val);
        if(v==0 || v == Number.NaN) return '0 %';
        return v.toFixed(2) + ' %';
    },
    rendDate:function(val){
        if(val==null) return '';
        var year = val.getFullYear();
        var month = val.getMonth()+1;
        var day = val.getDate();
        return year + '-' + (month<10?'0':'') + month + '-' + (day<10?'0':'') + day;
    },
	rendDateTime:function(val){
		return Ext.Date.format(val,'Y-m-d H:i:s');
	},
    getValue:function(arr,val){
        for(var i=0;i<arr.length;i++){
            if(arr[i].value == val){
                return arr[i].text;
            }
        }
        return '';
    },
	getMonthDay:function(val){
        if(val==null) val = new Date();
        var year = val.getFullYear(),
			month = val.getMonth(),
			nyear = (month==11)?year+1:year,
			nmonth = (month==11)?0:month+1,
			dtStart = new Date(year,month,1),
			dtEnd = new Date(nyear,nmonth,1),
			dtMsec = dtEnd.getTime()-dtStart.getTime();
		return Math.floor(dtMsec/(1000*60*60*24));
	},
    stateType:function(){
		var lang = Zm.i18n.Lang;
        return [
            {text:lang.tSFlag[0],value:0},
            {text:lang.tSFlag[1],value:1},
            {text:lang.tSFlag[2],value:-1}
        ];
    },
	rendStat:function(val){
		var cm = Zm.Common,
			arr = cm.stateType();
		return cm.getValue(arr,val);
	},
	rendFType:function(val){
		var cm = Zm.Common,
			arr = cm.ftypeArr();
		return cm.getValue(arr,val);
	},
	rendMultLine:function(value, meta, record){
		meta.style="white-space:normal;";     
        return value; 
	},
    rendVType:function(val){
        return Zm.Common.getValue(Zm.i18n.Lang.tVTypes,val);
    },
    
    getEditor:function(typedata,isDev){
        var editor,
            edtType = typedata.editor;
        if(edtType == 'combo'){
            editor = new Ext.grid.CellEditor({ field: {
                xtype:'combo',
                valueField:'id',
                displayField:'text',
                editable:false,
                store:Ext.create('Zm.store.EnumStore',{vtid:typedata.vtid})
            } });
        }else if(edtType=='devfield'){
            if(isDev) return null;
            editor = new Ext.grid.CellEditor({ field: {
                xtype:'combo',
                valueField:'id',
                displayField:'text',
                editable:false,
                store:Ext.create('Zm.store.DevDefEnum')
            } });
        }else if(edtType=='data'){
            editor = new Ext.grid.CellEditor({ field: {
                xtype:'combo',
                valueField:'id',
                displayField:'text',
                editable:false,
                store:Ext.create('Zm.store.DataCol'),
                matchFieldWidth:false,
                listConfig:{
                    width:300
                }
            } });
        }else if(edtType=='numberfield'){
            editor = new Ext.grid.CellEditor({ field: {xtype:typedata.editor,decimalPrecision:4} });
        }else{
            editor = new Ext.grid.CellEditor({ field: {xtype:typedata.editor} });
        }
        return editor;
    }
});